package com.financial.business.balance.service.gy.impl;

import com.financial.business.balance.config.GYAccessTokenConfig;
import com.financial.business.balance.config.UrlConfig;
import com.financial.business.balance.connector.SysconfigFeign;
import com.financial.business.balance.constants.FinancialPayBalanceEnumConstants;
import com.financial.business.balance.dto.FinancialGYBalancePayQueryDTO;
import com.financial.business.balance.service.gy.FinancialGYBalancePayQueryService;
import com.financial.business.balance.utils.*;
import com.financial.business.balance.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //够样余额支付查询接口
 * @Date 2019/12/6 17:05
 */
@Service
public class FinancialGYBalancePayQueryServiceImpl implements FinancialGYBalancePayQueryService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialGYBalancePayQueryServiceImpl.class);

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysconfigFeign sysconfigFeign;

    @Autowired
    private GYAccessTokenConfig gyAccessTokenConfig;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Override
    public ApiResult payQuery(String requestParam) {
        logger.info("FinancialGYBalancePayQueryServiceImpl.payQuery---接收够样余额支付查询参数json" + requestParam);
        FinancialGYBalancePayQueryDTO financialGYBalancePayQueryDTO = JsonUtils.convertBean(requestParam, FinancialGYBalancePayQueryDTO.class);

        Map<String, Object> params = JsonUtils.jsonToMap(requestParam);
        /*三方支付专用的交易单号*/
        Object tempParam = params.get("out_trade_no");
        String outTradeNo = tempParam == null ? null : tempParam.toString();
        /*业务类型*/
        tempParam = params.get("business_type");
        String businessType = tempParam == null ? null : tempParam.toString();
        /*现在支付渠道类型编码*/
        tempParam = params.get("pay_channel_no");
        String payChannelNo = tempParam == null ? null : tempParam.toString();
        /*对接平台编码*/
        tempParam = params.get("platform_no");
        String platformNo = tempParam == null ? null : tempParam.toString();
        if (StringUtils.isBlank(outTradeNo) || StringUtils.isBlank(businessType)
                || StringUtils.isBlank(platformNo) || StringUtils.isBlank(payChannelNo)) {
            /*对所传字段进行非空校验判断*/
            return ApiResult.failure(ValidateUtils.validateProperty(financialGYBalancePayQueryDTO));
        }
        String resultStr = "";
        /*获取够样支付平台信息*/
        ApiResult apiResult = sysconfigFeign.platfromInfo(payChannelNo);
        String apiResultMsg = (String) apiResult.getResult();
        //缓存中获取token
        String resultAccessToken = null;
        Object object = redisUtils.getValue("custumerAccessToken");
        if (apiResult.getCode() == FinancialPayBalanceEnumConstants.ENUM_CODE_SUC || (apiResultMsg != null && apiResultMsg != "")) {
            /*获取每个参数信息*/
            Map<String, Object> map = JsonUtils.jsonToMap(apiResultMsg);
            String platfromNo = (String) map.get("platfrom_no");
            String platformName = (String) map.get("platform_name");
            String passWord = (String) map.get("pass_word");
            String secret = (String) map.get("secret");
            if (null == object) {
                String sign = MD5Utils.md5(platformName + platfromNo + passWord + secret);
                resultAccessToken = gyAccessTokenConfig.getGYAccessToken(platformName, passWord, secret, sign);
                logger.info("FinancialGYBalancePayQueryServiceImpl.payQuery---余额支付获取金融中心token" + resultAccessToken);
            } else {
                resultAccessToken = (String) object;
            }
            Map<String, Object> paraMap = new HashMap<>();
            paraMap.put("accessToken", resultAccessToken);
            paraMap.put("orderNum", outTradeNo);
            try {
                String result = httpClientUtils.doPost(UrlConfig.BALANCE_PAY_QUERY_URL, paraMap);
                /*判断够样余额查询回执*/
                if (!FinancialPayBalanceEnumConstants.ENUM_GYBALANCEPAY_FAIL.equals(result)) {
                    /*解析返回信息判断状态*/
                    Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
                    /*1---成功标识*/
                    Boolean state = resultMap.containsKey("state");
                    if (state && (resultMap.get("state").toString()).equalsIgnoreCase(FinancialPayBalanceEnumConstants.ENUM_GYRESULTSTATE_SUC)) {
                        /*返回成功信息*/
                        logger.info("FinancialGYBalancePayQueryServiceImpl.payQuery---调用够样余额主动查询返回信息" + resultStr);
                        return ApiResult.success(result);
                    }
                } else {
                    logger.info("FinancialGYBalancePayQueryServiceImpl.payQuery---调用够样余额主动查询返回信息" + resultStr);
                    String resultExe = "请输入正确的单号信息";
                    return ApiResult.failure(resultExe);
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("FinancialGYBalancePayQueryServiceImpl.payQuery---查询预支付信息失败!" + JsonUtils.objectToJson(paraMap));
            }
        }
        return ApiResult.failure();
    }
}
